<template>
  <div class="mod-prod prod-list-mod">
    <!-- 查询和其他操作 -->
    <div class="search-bar">

      <el-form :inline="true" label-width="auto" class="search-form" size="small">

        <div class="input-row">

          <el-form-item label="分类名">
            <el-input
              clearable
              @keyup.enter.native="handleFind"
              class="filter-item"
              style="width: 200px;"
              v-model="keyword"
              placeholder="请输入分类名"
            ></el-input>
          </el-form-item>
          <div class="default-btn primary-btn" @click="handleFind">{{$t('crud.searchBtn')}}</div>
          <div class="default-btn" @click="resetSearchForm()">{{$t('crud.reset')}}</div>
        </div>
      </el-form>
    </div>

    <div class="main-container">
      <div class="operation-bar">
        <el-row :gutter="10" style="margin-bottom: 8px;">
          <div
            class="default-btn"
            @click="handleAdd">
            添加分类
          </div>
        </el-row>
      </div>

      <div class="table-con prod-table">
        <el-table :data="tableData"
                  style="width: 100%"
                  >

          <el-table-column label="序号" width="60" align="center">
            <template slot-scope="scope">
              <span>{{scope.$index + 1}}</span>
            </template>
          </el-table-column>

          <el-table-column label="分类名称" width="150" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.name }}</span>
            </template>
          </el-table-column>
          <el-table-column label="分类编码" width="150" align="center" prop="updateTime" >
            <template slot-scope="scope">
              <span>{{ scope.row.code }}</span>
            </template>
          </el-table-column>

          <el-table-column label="分类备注" width="200" align="center" prop="updateTime" >
            <template slot-scope="scope">
              <span>{{ scope.row.remark }}</span>
            </template>
          </el-table-column>
          <el-table-column label="排序" width="100" align="center" prop="sort" >
            <template slot-scope="scope">
              <el-tag type="warning">{{ scope.row.sort }}</el-tag>
            </template>
          </el-table-column>

          <el-table-column label="创建时间" width="160" align="center" prop="createTime" s>
            <template slot-scope="scope">
              <span>{{ scope.row.createTime }}</span>
            </template>
          </el-table-column>



          <el-table-column label="状态" width="100" align="center">
            <template slot-scope="scope">
              <template v-if="scope.row.status == 0">
                <span>已启用</span>
              </template>
              <template v-if="scope.row.status == 1">
                <span>已删除</span>
              </template>
              <template v-if="scope.row.status == 2">
                <span>已停用</span>
              </template>
            </template>
          </el-table-column>

          <el-table-column label="操作" fixed="right" min-width="230">
            <template slot-scope="scope">
              <el-button @click="handleEdit(scope.row)" v-if="scope.row.status != 1" type="primary" size="small">编辑
              </el-button>
              <el-button @click="handleOpen(scope.row)" v-if="scope.row.status == 2" type="success" size="small">启动
              </el-button>
              <el-button @click="handleClose(scope.row)" v-if="scope.row.status == 0" type="warning" size="small">停用
              </el-button>
              <el-button @click="handleDelete(scope.row)" type="danger" size="small"  v-if="scope.row.status != 1" >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="800px">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="分类名称" :label-width="formLabelWidth" prop="content">
          <el-input v-model="form.name" auto-complete="off" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="分类编码" :label-width="formLabelWidth" prop="content">
          <el-input v-model="form.code" auto-complete="off" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="分类备注" :label-width="formLabelWidth" prop="content">
          <el-input v-model="form.remark" auto-complete="off" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="排序" :label-width="formLabelWidth" prop="sort">
          <el-input v-model="form.sort" auto-complete="off" maxlength="10"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        tableData: [],
        keyword: '',
        title: '增加分类',
        dialogFormVisible: false, //控制弹出框
        formLabelWidth: '120px',
        isEditForm: false,
        form: {
          name: '',
          code: ''
        },
        rules: {
          name: [
            {required: true, message: '名称不能为空', trigger: 'blur'},
            {min: 1, max: 20, message: '长度在1到20个字符'},
          ],
          code: [
            {required: true, message: '编码不能为空', trigger: 'blur'},
            {min: 1, max: 20, message: '长度在1到20个字符'},
          ],
          sort: [
            {required: false, message: '排序字段不能为空', trigger: 'blur'},
            {pattern: /^[0-9]\d*$/, message: '排序字段只能为自然数'},
          ]
        }
      }
    },
    created () {
      this.tagList()
    },
    methods: {
      tagList: function () {
        var params = {}
        params.name = this.keyword

        this.$http({
          url: this.$http.adornUrl('/admin/ecosphere/ecosphereClassification/getAllEcosphereClassification'),
          method: 'get',
          params: this.$http.adornParams(params)
        }).then(({ data }) => {
          this.tableData = data
        })
      },
      getFormObject: function () {
        var formObject = {
          id: null,
          name: null,
          code: null,
          sort: this.tableData.length + 1
        }
        return formObject
      },

      //搜索
      handleFind: function () {
        this.tagList()
      },

      //重置
      resetSearchForm(){
        this.keyword = ''

        this.tagList()
      },



      handleAdd: function () {
        this.title = '增加分类'
        this.dialogFormVisible = true
        this.form = this.getFormObject()
        this.isEditForm = false
      },
      handleEdit: function (row) {
        this.title = '编辑分类'
        this.dialogFormVisible = true
        this.isEditForm = true
        this.form = JSON.parse(JSON.stringify(row))
      },
      handleClose: function (row) {
        var that = this
        row.status = 2,
          that.$http({
            url: that.$http.adornUrl(`/admin/ecosphere/ecosphereClassification/updateOneById`),
            method: 'put',
            data: this.$http.adornData(
              row)
          }).then(response => {
            if (response.code == '00000') {
              that.$message({
                message: that.$i18n.t('publics.operation'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  that.tagList()
                }
              })
            } else {
              that.$commonUtil.message.error(response.message)
            }
          })
      },
      handleOpen: function (row) {
        var that = this
        row.status = 0,
          that.$http({
            url: that.$http.adornUrl(`/admin/ecosphere/ecosphereClassification/updateOneById`),
            method: 'put',
            data: this.$http.adornData(
              row)
          }).then(response => {
            if (response.code == '00000') {
              that.$message({
                message: that.$i18n.t('publics.operation'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  that.tagList()
                }
              })
            } else {
              that.$commonUtil.message.error(response.message)
            }
          })
      },
      handleDelete: function (row) {
        var that = this
        this.$confirm('此操作将把分类删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            that.delete(row)
          })
          .catch(() => {
            this.$commonUtil.message.info('已取消删除')
          })
      },
      delete: function (row) {
        var that = this
        row.status = 1,
        that.$http({
          url: that.$http.adornUrl(`/admin/ecosphere/ecosphereClassification/updateOneById`),
          method: 'put',
          data: this.$http.adornData(
          row)
        }).then(response => {
          if (response.code == '00000') {
            that.$message({
              message: that.$i18n.t('publics.operation'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                that.tagList()
              }
            })
          } else {
            that.$commonUtil.message.error(response.message)
          }
        })
      },
      submitForm: function () {
        this.$refs.form.validate((valid) => {
          if (!valid) {
            console.log('校验失败')
            return
          } else {
            if (this.isEditForm) {
              this.$http({
                url: this.$http.adornUrl('/admin/ecosphere/ecosphereClassification/updateOneById'),
                method: 'put',
                data: this.$http.adornData(
                  this.form)
              }).then(({ data }) => {
                this.$message({
                  message: this.$i18n.t('publics.operation'),
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.dialogFormVisible = false
                    this.tagList()
                  }
                })
              })
            } else {
              this.$http({
                url: this.$http.adornUrl('/admin/ecosphere/ecosphereClassification/save'),
                method: 'post',
                data: this.$http.adornData(
                  this.form)
              }).then(({ data }) => {
                this.$message({
                  message: this.$i18n.t('publics.operation'),
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.dialogFormVisible = false
                    this.tagList()
                  }
                })
              })
            }
          }
        })
      },
      // 改变多选
      handleSelectionChange (val) {
        this.multipleSelection = val
      }
    }
  }
</script>
